package br.com.util.setup;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.arquitetura.controlador.AbstractMBean;
import br.com.arquitetura.excecoes.BDException;
import br.com.data.hibernate.AlunoHibernateDAO;
import br.com.model.entidade.Aluno;

@FacesConverter(value="alunoConverter")
public class AlunoConverter extends AbstractMBean<Aluno> implements Converter{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private AlunoHibernateDAO alunoHibernateDao;
	
	public AlunoConverter() {
		// TODO Auto-generated constructor stub
		alunoHibernateDao = (AlunoHibernateDAO) getBean("alunoHibernateDAO");
	}

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String value) {
		
		Aluno aluno = new Aluno(); 
		if ((value != null) && (!value.equals(""))) {
			System.out.println("O valor eh " + value);
			try {
				Long num = new Long(value);
				aluno = alunoHibernateDao.getById(Aluno.class,num);
			} catch (BDException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return aluno;
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object values) {
		if (values != null) {
			
			Aluno aluno = new Aluno();
			aluno = (Aluno) values;
			System.out.println("entrei no get string");
			System.out.println(aluno.getId());
			return aluno.getId().toString();
		}
		
		
		return null;
	}

	
	
}
